#!/bin/bash
# .........................................................................
# Title: Master_Build.sh
#
# Main executable file for build section; submits all build jobs via SLURM
# .........................................................................

# email notification setting
mailtype="ALL"

# job settings
nodes=1

# second argument is job name
job=${2}

# if user has shell profile setup
if [ -f ~/shell_profile.sh ]; then

    # run profile
    source ~/shell_profile.sh

    # set user variables
    U=${whoami}
    echo "User: "${U}

    # basic variables
    system_part="<SLURM_PARTITION>"

    # paths
    code_path="<CODE_PATH>"
    erroroutput_path="$project_path/erroroutput"

    # make error output folder if it doesn't exist
    mkdir -p "${erroroutput_path}"

    # job parameters
    partition_Build="<SLURM_PARTITION>"
    time_Build="0-72:00:00"
    ntasks_Build=12
    mem_Build="120000"

else

    echo "Please set up ~/shell_profile.sh file. Exiting."
    exitcd

fi

# sanity-checking that project_path variables exist
if [[ -z "$project_path" ]]; then
    echo "Empty project_path variable: Exiting..."
    exit 1
fi

#----------------
# Submit jobs
#----------------

# Moody's DRD step
Moodys_DRD_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Moodys_DRD \
		 --output="${erroroutput_path}/Moodys_DRD-%A_%a.out" --error="${erroroutput_path}/Moodys_DRD-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Moodys_DRD | awk '{print $NF}'`
echo "Submitted Moodys_DRD Job: "${Moodys_DRD_ID}
sleep 1

# Factset DCS step
Factset_DCS_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Factset_DCS \
		 --output="${erroroutput_path}/Factset_DCS-%A_%a.out" --error="${erroroutput_path}/Factset_DCS-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Moodys_DRD_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Factset_DCS | awk '{print $NF}'`
echo "Submitted Factset_DCS Job: "${Factset_DCS_ID}
sleep 1

# Mergent step
Mergent_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Mergent \
         --output="${erroroutput_path}/Mergent-%A_%a.out" --error="${erroroutput_path}/Mergent-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Factset_DCS_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Mergent | awk '{print $NF}'`
echo "Submitted Mergent Job: "${Mergent_ID}
sleep 1

# Capital IQ step
CIQ_Characteristics_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=CIQ_Characteristics \
         --output="${erroroutput_path}/CIQ_Characteristics-%A_%a.out" --error="${erroroutput_path}/CIQ_Characteristics-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Mergent_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} CIQ_Characteristics | awk '{print $NF}'`
echo "Submitted CIQ_Characteristics Job: "${CIQ_Characteristics_ID}
sleep 1

# Macro series step
Macro_Series_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Macro_Series \
         --output="${erroroutput_path}/Macro_Series-%A_%a.out" --error="${erroroutput_path}/Macro_Series-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${CIQ_Characteristics_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Macro_Series | awk '{print $NF}'`
echo "Submitted Macro_Series Job: "${Macro_Series_ID}
sleep 1

# Dealogic step
Dealogic_DCM_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Dealogic_DCM \
         --output="${erroroutput_path}/Dealogic_DCM-%A_%a.out" --error="${erroroutput_path}/Dealogic_DCM-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Macro_Series_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Dealogic_DCM | awk '{print $NF}'`
echo "Submitted Dealogic_DCM Job: "${Dealogic_DCM_ID}
sleep 1

# Sovereign data step
Sovereign_Data_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Sovereign_Data \
         --output="${erroroutput_path}/Sovereign_Data-%A_%a.out" --error="${erroroutput_path}/Sovereign_Data-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Dealogic_DCM_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Sovereign_Data | awk '{print $NF}'`
echo "Submitted Sovereign_Data Job: "${Sovereign_Data_ID}
sleep 1

# WRDS step
WRDS_Characteristics_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=WRDS_Characteristics \
         --output="${erroroutput_path}/WRDS_Characteristics-%A_%a.out" --error="${erroroutput_path}/WRDS_Characteristics-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Sovereign_Data_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} WRDS_Characteristics | awk '{print $NF}'`
echo "Submitted WRDS_Characteristics Job: "${WRDS_Characteristics_ID}
sleep 1

# Compustat step
Compustat_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Compustat \
         --output="${erroroutput_path}/Compustat-%A_%a.out" --error="${erroroutput_path}/Compustat-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${WRDS_Characteristics_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Compustat | awk '{print $NF}'`
echo "Submitted Compustat Job: "${Compustat_ID}
sleep 1

# Morningstar industry step
Morningstar_Industry_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Morningstar_Industry \
         --output="${erroroutput_path}/Morningstar_Industry-%A_%a.out" --error="${erroroutput_path}/Morningstar_Industry-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Compustat_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Morningstar_Industry | awk '{print $NF}'`
echo "Submitted Morningstar_Industry Job: "${Morningstar_Industry_ID}
sleep 1

# Fund holdings summary step
Fund_Holdings_Summary_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Fund_Holdings_Summary \
         --output="${erroroutput_path}/Fund_Holdings_Summary-%A_%a.out" --error="${erroroutput_path}/Fund_Holdings_Summary-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Morningstar_Industry_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Fund_Holdings_Summary | awk '{print $NF}'`
echo "Submitted Fund_Holdings_Summary Job: "${Fund_Holdings_Summary_ID}
sleep 1

# Interest details step
Interest_Details_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Interest_Details \
         --output="${erroroutput_path}/Interest_Details-%A_%a.out" --error="${erroroutput_path}/Interest_Details-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Fund_Holdings_Summary_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Interest_Details | awk '{print $NF}'`
echo "Submitted Interest_Details Job: "${Interest_Details_ID}
sleep 1

# TRACE step
Process_TRACE_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Process_TRACE \
         --output="${erroroutput_path}/Process_TRACE-%A_%a.out" --error="${erroroutput_path}/Process_TRACE-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Interest_Details_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Process_TRACE | awk '{print $NF}'`
echo "Submitted Process_TRACE Job: "${Process_TRACE_ID}
sleep 1

# Duration step
Consolidate_Duration_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Consolidate_Duration \
         --output="${erroroutput_path}/Consolidate_Duration-%A_%a.out" --error="${erroroutput_path}/Consolidate_Duration-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Process_TRACE_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Consolidate_Duration | awk '{print $NF}'`
echo "Submitted Consolidate_Duration Job: "${Consolidate_Duration_ID}
sleep 1

# Excess returns step
Excess_Returns_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Excess_Returns \
         --output="${erroroutput_path}/Excess_Returns-%A_%a.out" --error="${erroroutput_path}/Excess_Returns-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Consolidate_Duration_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Excess_Returns | awk '{print $NF}'`
echo "Submitted Excess_Returns Job: "${Excess_Returns_ID}
sleep 1

# Amounts outstanding step
Amounts_Outstanding_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Amounts_Outstanding \
         --output="${erroroutput_path}/Amounts_Outstanding-%A_%a.out" --error="${erroroutput_path}/Amounts_Outstanding-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Excess_Returns_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Amounts_Outstanding | awk '{print $NF}'`
echo "Submitted Amounts_Outstanding Job: "${Amounts_Outstanding_ID}
sleep 1

# Consolidate holdings step
Consolidate_Holdings_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Consolidate_Holdings \
         --output="${erroroutput_path}/Consolidate_Holdings-%A_%a.out" --error="${erroroutput_path}/Consolidate_Holdings-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Amounts_Outstanding_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Consolidate_Holdings | awk '{print $NF}'`
echo "Submitted Consolidate_Holdings Job: "${Consolidate_Holdings_ID}
sleep 1

# Ratings step
Consolidate_Ratings_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Consolidate_Ratings \
         --output="${erroroutput_path}/Consolidate_Ratings-%A_%a.out" --error="${erroroutput_path}/Consolidate_Ratings-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Consolidate_Holdings_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Consolidate_Ratings | awk '{print $NF}'`
echo "Submitted Consolidate_Ratings Job: "${Consolidate_Ratings_ID}
sleep 1

# Merged panel step
Price_Characteristics_Panel_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Price_Characteristics_Panel \
         --output="${erroroutput_path}/Price_Characteristics_Panel-%A_%a.out" --error="${erroroutput_path}/Price_Characteristics_Panel-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Consolidate_Ratings_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Price_Characteristics_Panel | awk '{print $NF}'`
echo "Submitted Price_Characteristics_Panel Job: "${Price_Characteristics_Panel_ID}
sleep 1

# Bond betas step
Bond_Betas_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Bond_Betas \
         --output="${erroroutput_path}/Bond_Betas-%A_%a.out" --error="${erroroutput_path}/Bond_Betas-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Price_Characteristics_Panel_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Bond_Betas | awk '{print $NF}'`
echo "Submitted Bond_Betas Job: "${Bond_Betas_ID}
sleep 1

# Industry master step
Industry_Master_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Industry_Master \
         --output="${erroroutput_path}/Industry_Master-%A_%a.out" --error="${erroroutput_path}/Industry_Master-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Bond_Betas_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Industry_Master | awk '{print $NF}'`
echo "Submitted Industry_Master Job: "${Industry_Master_ID}
sleep 1

# Insurance panel step
Insurance_Panel_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Insurance_Panel \
         --output="${erroroutput_path}/Insurance_Panel-%A_%a.out" --error="${erroroutput_path}/Insurance_Panel-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Industry_Master_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Insurance_Panel | awk '{print $NF}'`
echo "Submitted Insurance_Panel Job: "${Insurance_Panel_ID}
sleep 1

# Bartik step
Bartik_Instrument_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Bartik_Instrument \
         --output="${erroroutput_path}/Bartik_Instrument-%A_%a.out" --error="${erroroutput_path}/Bartik_Instrument-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Insurance_Panel_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Bartik_Instrument | awk '{print $NF}'`
echo "Submitted Bartik_Instrument Job: "${Bartik_Instrument_ID}
sleep 1

# Drawdowns step
Drawdowns_Sample_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Drawdowns_Sample \
         --output="${erroroutput_path}/Drawdowns_Sample-%A_%a.out" --error="${erroroutput_path}/Drawdowns_Sample-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Bartik_Instrument_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Drawdowns_Sample | awk '{print $NF}'`
echo "Submitted Drawdowns_Sample Job: "${Drawdowns_Sample_ID}
sleep 1

# Firm outcomes step
Firm_Outcomes_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Firm_Outcomes \
         --output="${erroroutput_path}/Firm_Outcomes-%A_%a.out" --error="${erroroutput_path}/Firm_Outcomes-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Drawdowns_Sample_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} Firm_Outcomes | awk '{print $NF}'`
echo "Submitted Firm_Outcomes Job: "${Firm_Outcomes_ID}
sleep 1

# Firm sample step
Consolidate_Firm_Sample_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=Consolidate_Firm_Sample \
         --output="${erroroutput_path}/Consolidate_Firm_Sample-%A_%a.out" --error="${erroroutput_path}/Consolidate_Firm_Sample-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Firm_Outcomes_ID} \
         "${code_path}/build/Build_Python_Controller.sh" ${U} Consolidate_Firm_Sample | awk '{print $NF}'`
echo "Submitted Consolidate_Firm_Sample Job: "${Consolidate_Firm_Sample_ID}
sleep 1

# Non-US panel step
NonUS_Panel_ID=`sbatch \
         --partition=${partition_Build} --time=${time_Build} \
         --nodes=${nodes} --ntasks=${ntasks_Build} --job-name=NonUS_Panel \
         --output="${erroroutput_path}/NonUS_Panel-%A_%a.out" --error="${erroroutput_path}/NonUS_Panel-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_Build} \
         --depend=afterok:${Consolidate_Firm_Sample_ID} \
         "${code_path}/build/Build_Stata_Controller.sh" ${U} NonUS_Panel | awk '{print $NF}'`
echo "Submitted NonUS_Panel Job: "${NonUS_Panel_ID}
sleep 1

echo "All jobs submitted!"
